import { request } from "@umijs/max";

type QueryList = { keyword?: string; current?: number; pageSize?: number; } & Record<string, any>
export async function queryRoleList(params: QueryList, options?: { [key: string]: any },) {
    let data = await request<System.PageInfo<System.RoleInfo>>('/api/system/role/', {
        method: 'GET',
        params: {
            ...params,
            page: params.current,
            page_size: params.pageSize,
        },
        ...(options || {}),
    });
    return {
        data: data.datas || [],
        success: true,
        total: data.total_items,
    }
}

export async function queryRoleAll(options?: { [key: string]: any },) {
    let page = 1;
    let page_size = 50;
    let datas: System.RoleInfo[] = []
    for (; ;) {
        let data = await queryRoleList({ current: page, pageSize: page_size })
        datas = datas.concat(data.data.filter(d => d.status))
        if (page * page_size > data.total) {
            break
        } else {
            page += 1
        }
    }
    return datas
}

export async function addRole(
    body?: System.RoleInfo,
    options?: { [key: string]: any },
) {
    return request<any>('/api/system/role/', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        data: body,
        ...(options || {}),
    });
}

export async function updateRole(
    id: number,
    body: System.RoleInfo,
    options?: { [key: string]: any },
) {
    return request(`/api/system/role/${id}`, {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
        },
        data: body,
        ...(options || {}),
    });
}

export async function deleteRole(
    id: number,
    options?: { [key: string]: any },
) {
    return request(`/api/system/role/${id}`, {
        method: 'DELETE',
        ...(options || {}),
    });
}

export async function queryRoleResourceList(id: number | undefined,
    options?: { [key: string]: any }) {
    if (!id) return []
    return await request<System.RoleResourceInfo[]>(`/api/system/role/resource/${id}`, {
        method: 'GET',
        ...(options || {}),
    });
}

export async function updateRoleResource(id: number,
    data: { resource_id: number, method: string }[],
    options?: { [key: string]: any }) {
    return await request(`/api/system/role/resource/${id}`, {
        method: 'PUT',
        data,
        ...(options || {}),
    });
}

export async function queryAllResource(options?: { [key: string]: any }) {
    let page = 1;
    let page_size = 50;
    let datas: System.ResourceInfo[] = []
    for (; ;) {
        let data = await queryResourceList({ current: page, pageSize: page_size })
        datas = datas.concat(data.data)
        if (page * page_size > data.total) {
            break
        } else {
            page += 1
        }
    }
    return datas
}

export async function queryResourceList(params: QueryList, options?: { [key: string]: any },) {
    let data = await request<System.PageInfo<System.ResourceInfo>>('/api/system/resource/', {
        method: 'GET',
        params: {
            ...params,
            page: params.current,
            page_size: params.pageSize,
        },
        ...(options || {}),
    });
    return {
        data: data.datas || [],
        success: true,
        total: data.total_items,
    }
}

export async function addResource(
    body?: System.ResourceInfo,
    options?: { [key: string]: any },
) {
    return request<any>('/api/system/resource/', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        data: body,
        ...(options || {}),
    });
}

export async function updateResource(
    id: number,
    body: System.ResourceInfo,
    options?: { [key: string]: any },
) {
    return request(`/api/system/resource/${id}`, {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
        },
        data: body,
        ...(options || {}),
    });
}

export async function deleteResource(
    id: number,
    options?: { [key: string]: any },
) {
    return request(`/api/system/resource/${id}`, {
        method: 'DELETE',
        ...(options || {}),
    });
}

export async function queryUserList(
    params: {
        keyword?: string;
        current?: number;
        dept_id?: number;
        pageSize?: number;
    },
    sort: Record<string, 'descend' | 'ascend' | null>,
    options?: { [key: string]: any },
) {
    let data = await request<System.PageInfo<System.UserListInfo>>('/api/system/user/', {
        method: 'GET',
        params: {
            ...params,
            page: params.current,
            page_size: params.pageSize,
            dept_id: params.dept_id,
            order: sort.created_at
                ? sort.created_at === 'ascend'
                    ? 'created_at-asc'
                    : 'created_at-desc'
                : '',
        },
        ...(options || {}),
    });
    return {
        data: data.datas || [],
        success: true,
        total: data.total_items,
    }
}

export async function getUserDetail(
    id: number,
    options?: { [key: string]: any },
) {
    return request<System.UserInfo>(`/api/system/user/${id}`, {
        method: 'GET',
        ...(options || {}),
    });
}

export async function addUser(
    body: System.UserInfo,
    options?: { [key: string]: any },
) {
    return request<any>('/api/system/user/', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        data: body,
        ...(options || {}),
    });
}

export async function updateUser(
    id: number,
    body: System.UserInfo,
    options?: { [key: string]: any },
) {
    if (!body.password) {
        delete body.password
    }
    return request(`/api/system/user/${id}`, {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
        },
        data: body,
        ...(options || {}),
    });
}

export async function deleteUser(
    id: number,
    options?: { [key: string]: any },
) {
    return request(`/api/system/user/${id}`, {
        method: 'DELETE',
        ...(options || {}),
    });
}


export async function queryDeptList(
    params: {
        keyword?: string;
        current?: number;
        pageSize?: number;
        status?: boolean;
    },
    options?: { [key: string]: any },
) {
    let data = await request<System.DeptInfo>('/api/system/dept/', {
        method: 'GET',
        params: {
            ...params,
            page: params.current,
            page_size: params.pageSize,
        },
        ...(options || {}),
    });
    return {
        data: [data],
        success: true,
        total: 1,
    }
}

export async function getDeptInfo(id: number, options?: { [key: string]: any }) {
    return request<System.DeptInfo>(`/api/system/dept/${id}`, { method: 'GET', ...(options || {}), });
}

export async function addDept(
    body: System.DeptInfo,
    options?: { [key: string]: any },
) {
    return request('/api/system/dept/', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        data: body,
        ...(options || {}),
    });
}

export async function updateDept(
    id: number,
    body: System.DeptInfo,
    options?: { [key: string]: any },
) {
    return request(`/api/system/dept/${id}`, {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
        },
        data: body,
        ...(options || {}),
    });
}

export async function deleteDept(
    id: number,
    options?: { [key: string]: any },
) {
    return request(`/api/system/dept/${id}`, {
        method: 'DELETE',
        ...(options || {}),
    });
}


export async function queryLoginLogList(
    params: {
        keyword?: string;
        current?: number;
        pageSize?: number;
    },
    options?: { [key: string]: any },
) {
    let data = await request<System.PageInfo<System.LoginLogInfo>>('/api/system/log/login', {
        method: 'GET',
        params: {
            page: params.current,
            page_size: params.pageSize,
        },
        ...(options || {}),
    });
    return {
        data: data.datas || [],
        success: true,
        total: data.total_items,
    }
}

export async function queryOperateLogList(
    params: {
        keyword?: string;
        current?: number;
        pageSize?: number;
    },
    options?: { [key: string]: any },
) {
    let data = await request<System.PageInfo<System.OperateLogInfo>>('/api/system/log/operate', {
        method: 'GET',
        params: {
            page: params.current,
            page_size: params.pageSize,
        },
        ...(options || {}),
    });
    return {
        data: data.datas || [],
        success: true,
        total: data.total_items,
    }
}

export async function queryDictTypeList(params: QueryList, options?: { [key: string]: any },) {
    let data = await request<System.PageInfo<System.DictInfo>>('/api/system/dict/', {
        method: 'GET',
        params: { ...params, page: params.current, page_size: params.pageSize },
        ...(options || {}),
    });
    return {
        data: data.datas || [],
        success: true,
        total: data.total_items,
    }
}

export async function addDictType(
    body?: System.DictInfo,
    options?: { [key: string]: any },
) {
    return request<any>('/api/system/dict/', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        data: body,
        ...(options || {}),
    });
}

export async function updateDictType(
    id: number,
    body?: System.DictInfo,
    options?: { [key: string]: any },
) {
    return request<any>(`/api/system/dict/${id}`, {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
        },
        data: body,
        ...(options || {}),
    });
}

export async function deleteDictType(
    id: number,
    options?: { [key: string]: any },
) {
    return request<any>(`/api/system/dict/${id}`, {
        method: 'DELETE',
        headers: {
            'Content-Type': 'application/json',
        },
        ...(options || {}),
    });
}

export async function queryDictDateList(dataType: number, params: QueryList, options?: { [key: string]: any },) {
    let data = await request<System.PageInfo<System.DictDataInfo>>(`/api/system/dict/${dataType}/`, {
        method: 'GET',
        params: {
            ...params,
            page: params.current,
            page_size: params.pageSize,
        },
        ...(options || {}),
    });
    return {
        data: data.datas || [],
        success: true,
        total: data.total_items,
    }
}

export async function queryDictDateAllList(dataType: number, options?: { [key: string]: any },) {
    let page = 1;
    let page_size = 50;
    let datas: System.DictDataInfo[] = []
    for (; ;) {
        let data = await queryDictDateList(dataType, { current: page, pageSize: page_size })
        datas = datas.concat(data.data)
        if (page * page_size > data.total) {
            break
        } else {
            page += 1
        }
    }
    return datas
}

export async function addDictData(dict_id: number, body: System.DictDataInfo, options?: { [key: string]: any },) {
    return request<any>(`/api/system/dict/${dict_id}/`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        data: body,
        ...(options || {}),
    });
}

export async function updateDictData(dict_id: number, id: number, body: System.DictDataInfo, options?: { [key: string]: any },) {
    return request<any>(`/api/system/dict/${dict_id}/${id}`, {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
        },
        data: body,
        ...(options || {}),
    });
}

export async function deleteDictData(
    dict_id: number,
    id: number,
    options?: { [key: string]: any },
) {
    return request<any>(`/api/system/dict/${dict_id}/${id}`, {
        method: 'DELETE',
        headers: {
            'Content-Type': 'application/json',
        },
        ...(options || {}),
    });
}

export async function queryPostList(params: QueryList, options?: { [key: string]: any },) {
    let data = await request<System.PageInfo<System.PostInfo>>('/api/system/post/', {
        method: 'GET',
        params: {
            ...params,
            page: params.current,
            page_size: params.pageSize,
        },
        ...(options || {}),
    });
    return {
        data: data.datas || [],
        success: true,
        total: data.total_items,
    }
}

export async function queryPostAll(options?: { [key: string]: any },) {
    let page = 1;
    let page_size = 50;
    let datas: System.PostInfo[] = []
    for (; ;) {
        let data = await queryPostList({ current: page, pageSize: page_size })
        datas = datas.concat(data.data.filter(d => d.status))
        if (page * page_size > data.total) {
            break
        } else {
            page += 1
        }
    }
    return datas
}

export async function getPost(id: number, options?: { [key: string]: any },) {
    return request<System.PostInfo>(`/api/system/post/${id}`, { method: 'GET', ...(options || {}), });
}

export async function addPost(body?: System.PostDo, options?: { [key: string]: any },) {
    return request<any>('/api/system/post/', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', },
        data: body,
        ...(options || {}),
    });
}

export async function updatePost(id: number, body: System.PostDo, options?: { [key: string]: any }) {
    return request(`/api/system/post/${id}`, {
        method: 'PUT',
        headers: { 'Content-Type': 'application/json', },
        data: body,
        ...(options || {}),
    });
}

export async function deletePost(id: number, options?: { [key: string]: any },) {
    return request(`/api/system/post/${id}`, { method: 'DELETE', ...(options || {}) });
}